package com.it.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.it.aspect.SysLog;
import com.it.entity.Price;
import com.it.entity.Role;
import com.it.entity.User;
import com.it.mapper.RoleMapper;
import com.it.service.PriceService;
import com.it.vo.CustomerVO;
import com.it.entity.Customer;
import com.it.service.CustomerService;
import com.it.utils.DataGridViewResult;
import com.it.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author bxystart
 * @since 2021-04-04
 */
@RestController
@RequestMapping("/customer")
public class CustomerController {

    @Autowired
    private CustomerService customerService;


    /**
     * 客户模糊查询
     * @param
     * @return
     */
    @SysLog("客户查询操作")
    @RequestMapping("/customerOneList")
    public DataGridViewResult customerOneList(CustomerVO customerVO,HttpSession session) {
        User user = (User) session.getAttribute("username");
        //创建分页信息    参数1 当前页  参数2 每页显示条数
        IPage<Customer> page = new Page<>(customerVO.getPage(), customerVO.getLimit());
        QueryWrapper<Customer> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("uid",user.getUid());
        IPage<Customer> customerIPage = customerService.page(page, queryWrapper);
        /**
         * logsIPage.getTotal() 总条数
         * logsIPage.getRecords() 分页记录列表
         */
        return new DataGridViewResult(customerIPage.getTotal(),customerIPage.getRecords());

    }

    @Autowired
    private RoleMapper roleMapper;
    /**
     * 客户模糊查询
     * @param
     * @return
     */
    @SysLog("客户查询操作")
    @RequestMapping("/customerList")
    public DataGridViewResult customerList(CustomerVO customerVO,HttpSession session) {
        User user = (User) session.getAttribute("username");
        Role role = roleMapper.getRoleByUserId(user.getUid());



        //创建分页信息    参数1 当前页  参数2 每页显示条数
        IPage<Customer> page = new Page<>(customerVO.getPage(), customerVO.getLimit());
        QueryWrapper<Customer> queryWrapper = new QueryWrapper<>();

        if(role.getRolename().equals("支部管理员")){
            queryWrapper.eq("status",0);
        }else if(role.getRolename().equals("系统管理员")){
            queryWrapper.eq("systatus",0);
        }

        IPage<Customer> customerIPage = customerService.page(page, queryWrapper);
        /**
         * logsIPage.getTotal() 总条数
         * logsIPage.getRecords() 分页记录列表
         */
        return new DataGridViewResult(customerIPage.getTotal(),customerIPage.getRecords());

    }

    /**
     * 客户模糊查询
     * @param
     * @return
     */
    @SysLog("客户查询操作")
    @RequestMapping("/customerAllList")
    public DataGridViewResult customerAllList(CustomerVO customerVO) {



        //创建分页信息    参数1 当前页  参数2 每页显示条数
        IPage<Customer> page = new Page<>(customerVO.getPage(), customerVO.getLimit());
        QueryWrapper<Customer> queryWrapper = new QueryWrapper<>();
        if(StringUtils.isEmpty(customerVO.getCustname())){

        }else{
            queryWrapper.eq("custname",customerVO.getCustname());
        }
        IPage<Customer> customerIPage = customerService.page(page, queryWrapper);
        /**
         * logsIPage.getTotal() 总条数
         * logsIPage.getRecords() 分页记录列表
         */
        return new DataGridViewResult(customerIPage.getTotal(),customerIPage.getRecords());

    }

    /**
     * 客户批量删除
     * @param ids
     * @return
     */
    @SysLog("客户删除操作")
    @RequestMapping("/deleteList")
    public Result deleteList(String ids) {
        //将字符串拆分成数组
        String[] idsStr = ids.split(",");
        List<String> list = Arrays.asList(idsStr);
        boolean bool = customerService.removeByIds(list);
        if(bool){
            return Result.success(true,"200","删除成功！");
        }
        return Result.error(false,null,"删除失败！");
    }

    /**
     * 添加客户信息
     * @param customer
     * @return
     */
    @SysLog("客户添加操作")
    @PostMapping("/addcustomer")
    public Result addCustomer(Customer customer, HttpSession session){
        //

        User user = (User) session.getAttribute("username");

        Customer i = customerService.getOne(new QueryWrapper<Customer>().eq("uid",user.getUid()).eq("level",customer.getLevel()));
        if(i!=null){
            return Result.error(false,null,"您已提交了入党申请！");
        }

        if(customer.getLevel()==1){

        }else if(customer.getLevel()==2){
            Customer item = customerService.getOne(new QueryWrapper<Customer>().eq("uid",user.getUid()).eq("level",1).eq("status",1).eq("systatus",1));
            if(item!=null){

            }else{
                return Result.error(false,null,"入党积极分子审核暂未通过！");
            }
        }else if(customer.getLevel()==3){
            Customer item = customerService.getOne(new QueryWrapper<Customer>().eq("uid",user.getUid()).eq("level",2).eq("status",1).eq("systatus",1));
            if(item!=null&&item.getStatus()==1&&item.getSystatus()==1){

            }else{
                return Result.error(false,null,"预备党员审核暂未通过！");
            }
        }


        customer.setStatus(0);
        customer.setTime(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
        customer.setUid(user.getUid());
        boolean bool = customerService.save(customer);
        if(bool){
            return Result.success(true,"200","添加成功！");
        }
        return Result.error(false,null,"添加失败！");
    }

    /**
     * 修改客户信息
     * @param customer
     * @return
     */
    @SysLog("客户修改操作")
    @PostMapping("/updatecustomer")
    public Result updateCustomer(Customer customer){

        boolean bool = customerService.updateById(customer);
        if(bool){
            return Result.success(true,"200","修改成功！");
        }
        return Result.error(false,null,"修改失败！");
    }

    /**
     * 删除单条数据
     * @param id
     * @return
     */
    @SysLog("客户删除操作")
    @RequestMapping("/deleteOne")
    public Result deleteOne(int id) {

        boolean bool = customerService.removeById(id);
        if(bool){
            return Result.success(true,"200","删除成功！");
        }
        return Result.error(false,null,"删除失败！");
    }

    @Autowired
    private PriceService priceService;
    /**
     * 通过单条数据
     * @return
     */
    @SysLog("付款操作")
    @RequestMapping("/pay")
    public Result pay(HttpSession session) {
        User user = (User) session.getAttribute("username");

        Price provider = priceService.getOne(new QueryWrapper<Price>().eq("uid",user.getUid()));
        if(provider!=null){
            return Result.error(false,null,"已经支付过！");
        }

        Customer i = customerService.getOne(new QueryWrapper<Customer>().eq("uid",user.getUid()).eq("level",3).eq("status",1).eq("systatus",1));

        if(i!=null){

        }else{
            return Result.error(false,null,"您还不是正式党员！");
        }
        Price item = new Price();
        item.setUid(user.getUid());
        item.setPrice(new BigDecimal("50"));
        priceService.save(item);
        return Result.success(true,"200","支付成功！");
    }


    /**
     * 通过单条数据
     * @param id
     * @return
     */
    @SysLog("客户通过操作")
    @RequestMapping("/agreeOne")
    public Result agreeOne(int id, HttpSession session) {


        User user = (User) session.getAttribute("username");
        Role role = roleMapper.getRoleByUserId(user.getUid());
        Customer s = customerService.getById(id);

        if(role.getRolename().equals("支部管理员")){
            if(s.getStatus()!=0){
                return Result.error(false,null,"当前状态已审批");
            }
            boolean bool = customerService.update(new UpdateWrapper<Customer>()
                    .set("status",1)
                    .set("systatus",0)
                    .eq("custid",id)
            );
            if(bool){
                return Result.success(true,"200","通过成功！");
            }
        }else if(role.getRolename().equals("系统管理员")){
            if(s.getSystatus()!=0){
                return Result.error(false,null,"当前状态已审批");
            }
            boolean bool = customerService.update(new UpdateWrapper<Customer>()
                    .set("systatus",1)
                    .eq("custid",id)
            );
            if(bool){
                return Result.success(true,"200","通过成功！");
            }
        }



        return Result.error(false,null,"通过失败！");
    }

    /**
     * 拒绝单条数据
     * @param id
     * @return
     */
    @SysLog("客户拒绝操作")
    @RequestMapping("/disAgreeOne")
    public Result disAgreeOne(int id, HttpSession session) {
        User user = (User) session.getAttribute("username");
        Role role = roleMapper.getRoleByUserId(user.getUid());
        Customer s = customerService.getById(id);

        if(role.getRolename().equals("支部管理员")){
            if(s.getStatus()!=0){
                return Result.error(false,null,"当前状态已审批");
            }
            boolean bool = customerService.update(new UpdateWrapper<Customer>()
                    .set("status",1)
                    .eq("custid",id)
            );
            if(bool){
                return Result.success(true,"200","拒绝成功！");
            }
        }else if(role.getRolename().equals("系统管理员")){
            if(s.getSystatus()!=0){
                return Result.error(false,null,"当前状态已审批");
            }
            boolean bool = customerService.update(new UpdateWrapper<Customer>()
                    .set("systatus",1)
                    .eq("custid",id)
            );
            if(bool){
                return Result.success(true,"200","拒绝成功！");
            }
        }
        return Result.error(false,null,"拒绝失败！");
    }


    /**
     *
     * 加载下拉框
     * @return
     */
    @RequestMapping("/loadAllCustomer")
    public DataGridViewResult loadAllCustomer(){
        QueryWrapper<Customer> queryWrapper = new QueryWrapper<>();
        List<Customer> list = customerService.list(queryWrapper);
        return new DataGridViewResult(list);

    }

}
